package smartclass.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jeremyfeinstein.slidingmenu.example.R;

import smartclass.bussiness.HomeworkBussiness;
import smartclass.data.FinalValues;
import smartclass.entity.HomeworkInfo;
import smartclass.entity.TopicInfo;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandListHomeworkAdapter extends BaseExpandableListAdapter {

	private Context context;
	private Map<TopicInfo, List<HomeworkInfo>> homeworkCollections;
	private List<TopicInfo> topics;
	private String name;

	public ExpandListHomeworkAdapter(Context context, List<TopicInfo> topics,
			Map<TopicInfo, List<HomeworkInfo>> homeworkCollections) {
		this.context = context;
		this.homeworkCollections = homeworkCollections;
		this.topics = topics;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return homeworkCollections.get(topics.get(groupPosition)).get(
				childPosition);
	}

	@Override
	public long getChildId(int groupPostion, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final HomeworkInfo homework = (HomeworkInfo) getChild(groupPosition,
				childPosition);
		LayoutInflater inflater = LayoutInflater.from(context);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.child_item, parent, false);
		}
		TextView item = (TextView) convertView.findViewById(R.id.laptop);
		ImageView delete = (ImageView) convertView.findViewById(R.id.delete);
		delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setMessage("Do you want to remove?");
				builder.setCancelable(false);
				builder.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								List<HomeworkInfo> child = homeworkCollections
										.get(topics.get(groupPosition));
								child.remove(childPosition);
								notifyDataSetChanged();
							}
						});
				builder.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});

		item.setText(homework.getName());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		if (homeworkCollections.get(topics.get(groupPosition)) != null) {

			return homeworkCollections.get(topics.get(groupPosition)).size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return topics.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return topics.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final TopicInfo topic = (TopicInfo) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.group_item, null);
		}
		ImageView imgAdd = (ImageView) convertView.findViewById(R.id.add);
		imgAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				FinalValues.displayNotification(context, "Add");
				List<HomeworkInfo> child = homeworkCollections.get(topics
						.get(groupPosition));
				if (child == null) {
					FinalValues.displayNotification(context, "Child null");
					child = new ArrayList<HomeworkInfo>();
				}
				creatDialog(topic.getName(), topic, child);
			}
		});
		TextView item = (TextView) convertView.findViewById(R.id.laptop);
		item.setTypeface(null, Typeface.BOLD);
		item.setText(topic.getName());
		return convertView;

	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	public void creatDialog(String title, final TopicInfo topic,
			final List<HomeworkInfo> child) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		LayoutInflater inflater = LayoutInflater.from(context);
		builder.setIcon(R.drawable.gnome_mime_application_x_class_file);
		View v = inflater.inflate(R.layout.add_new_item_dialog, null);
		builder.setView(v);
		final EditText edtName = (EditText) v.findViewById(R.id.nameNewItem);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				name = edtName.getText().toString();
				// Add new Bai giang
				HomeworkInfo homework = new HomeworkInfo();

				HomeworkBussiness homeworkBussiness = HomeworkBussiness
						.getInstance();
				if (name.isEmpty()
						|| !homeworkBussiness.checkNameAvailable(name)) {
					FinalValues.displayNotification(context,
							"Name is not available");
					return;
				}

				homework.setName(name);
				homework.setIdTopic(topic.getId());
				homework.setPath(FinalValues.FILE_PATH_HOMEWORK + name + ".spd");
				homeworkBussiness.addHomework(homework);
				updateListChild(child, topic);
			}
		});
		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	private void updateListChild(List<HomeworkInfo> child, TopicInfo topic) {
		child = new ArrayList<HomeworkInfo>();
		HomeworkBussiness homeworkBussiness = HomeworkBussiness.getInstance();
		child = homeworkBussiness.getAllHomework(topic.getId());
		homeworkCollections.put(topic, child);
		notifyDataSetChanged();
	}
}
